package com.su.lightthings.activity

import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.hjq.permissions.OnPermissionCallback
import com.su.lightthings.R
import com.su.lightthings.adapter.HomeViewPagerAdapter
import com.su.lightthings.base.ViewActivity
import com.su.lightthings.databinding.ActivityMainBinding
import com.su.lightthings.fragment.AboutFragment
import com.su.lightthings.fragment.HomeFragment
import com.su.lightthings.fragment.SportFragment

class MainActivity : ViewActivity<ActivityMainBinding>() {

    companion object {
        private const val TAG = "MainActivity"
    }

    private val viewPagerAdapter by lazy {
        HomeViewPagerAdapter(this@MainActivity, fragmentList)
    }

    override fun getViewBinding(): ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)

    override fun initViews() {
        binding.viewpager2.adapter = viewPagerAdapter
        binding.navView.setOnItemSelectedListener {
            val itemId = it.itemId
            when (itemId) {
                R.id.navigation_home ->
                    binding.viewpager2.currentItem = 0

                R.id.navigation_sport ->
                    binding.viewpager2.currentItem = 1

                R.id.navigation_about ->
                    binding.viewpager2.currentItem = 2
            }
            true
        }
        binding.viewpager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    0 ->
                        binding.navView.selectedItemId = R.id.navigation_home

                    1 ->
                        binding.navView.selectedItemId = R.id.navigation_sport

                    2 ->
                        binding.navView.selectedItemId = R.id.navigation_about
                }
            }
        })
    }

    private val fragmentList = arrayListOf<Fragment>().apply {
        add(HomeFragment())
        add(SportFragment())
        add(AboutFragment())
    }


}